From d70ca2108f88b0deaf7a6793aba3554abccee770 Mon Sep 17 00:00:00 2001 From: Anton Larin Date: Thu, 23 Mar 2017 17:26:40 +0300 Subject: [PATCH] Include package props with env vars into target metadata Previously, when changing package properties with corresponding environment variables (such as authors, which has CARGO_PKG_AUTHORS), it didn't invalidate the build, even though there could have been a dependency on such variables in the source code. This commit includes such properties (there are 3 of them in total: authors, description and homepage) in the target metadata. Fixes #3696. --- src/cargo/ops/cargo_rustc/compilation.rs | 4 +++ src/cargo/ops/cargo_rustc/context.rs | 7 ++++ tests/freshness.rs | 41 ++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 37bf4008f..5192d2cad 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -146,6 +146,10 @@ impl<'cfg> Compilation<'cfg> { let cargo_exe = self.config.cargo_exe()?; cmd.env(::CARGO_ENV, cargo_exe); + // When adding new environment variables depending on + // crate properties which might require rebuild upon change + // consider adding the corresponding properties to the hash + // in Context::target_metadata() cmd.env("CARGO_MANIFEST_DIR", pkg.root()) .env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string()) .env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string()) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 756022dbc..b5d01cd3e 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -399,6 +399,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> { // to pull crates from anywhere w/o worrying about conflicts unit.pkg.package_id().hash(&mut hasher); + // Add package properties which map to environment variables + // exposed by Cargo + let manifest_metadata = unit.pkg.manifest().metadata(); + manifest_metadata.authors.hash(&mut hasher); + manifest_metadata.description.hash(&mut hasher); + manifest_metadata.homepage.hash(&mut hasher); + // Also mix in enabled features to our metadata. This'll ensure that // when changing feature sets each lib is separately cached. self.resolve.features_sorted(unit.pkg.package_id()).hash(&mut hasher); diff --git a/tests/freshness.rs b/tests/freshness.rs index fc95cd883..eb7532b34 100644 --- a/tests/freshness.rs +++ b/tests/freshness.rs @@ -680,3 +680,44 @@ fn rebuild_if_build_artifacts_move_forward_in_time() { [FINISHED] [..] ")); } + +#[test] +fn rebuild_if_environment_changes() { + let p = project("env_change") + .file("Cargo.toml", r#" + [package] + name = "env_change" + description = "old desc" + version = "0.0.1" + authors = [] + "#) + .file("src/main.rs", r#" + fn main() { + println!("{}", env!("CARGO_PKG_DESCRIPTION")); + } + "#); + + assert_that(p.cargo_process("run"), + execs().with_status(0) + .with_stdout("old desc").with_stderr(&format!("\ +[COMPILING] env_change v0.0.1 ({dir}) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] `target[/]debug[/]env_change[EXE]` +", dir = p.url()))); + + File::create(&p.root().join("Cargo.toml")).unwrap().write_all(br#" + [package] + name = "env_change" + description = "new desc" + version = "0.0.1" + authors = [] + "#).unwrap(); + + assert_that(p.cargo("run"), + execs().with_status(0) + .with_stdout("new desc").with_stderr(&format!("\ +[COMPILING] env_change v0.0.1 ({dir}) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] `target[/]debug[/]env_change[EXE]` +", dir = p.url()))); +} -- 2.30.2